package com.yjs.app.trade.core.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.core.io.ClassPathResource;

public class PropertiesUtils {

	private static Properties properties;
	
	private static Map<String, Properties> map = new HashMap<>();
	
	public static String getProperty(String key) {
		try {
			if (properties == null) {
				ClassPathResource classPathResource = new ClassPathResource("application.properties");
				properties = new Properties();
				properties.load(classPathResource.getInputStream());
			}
			return properties.getProperty(key);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static String getProjectName () {
		return getProperty("project.name");
	}
	
	public static String getOtherProperty (String propertyName, String key) {
		if (map.get(propertyName) != null) {
			return map.get(propertyName).getProperty(key);
		} else {
			ClassPathResource classPathResource = new ClassPathResource(propertyName);
			Properties properties = new Properties();
			try {
				properties.load(classPathResource.getInputStream());
			} catch (IOException e) {
				e.printStackTrace();
			}
			map.put(propertyName, properties);
			return properties.getProperty(key);
		}
	}
}
